package cn.kgc.crowdfundingadmin3.controller;

import cn.kgc.crowdfundingadmin3.common.CrowdFundingConstant;
import cn.kgc.crowdfundingadmin3.common.ResultEntity;
import cn.kgc.crowdfundingadmin3.pojo.Admin;
import cn.kgc.crowdfundingadmin3.service.AdminService;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class AdminController {
    @Resource
    AdminService adminService;
    @RequestMapping("/admin/do/login")
    public String doLogin( @RequestParam("loginAcct") String loginAcct,
                           @RequestParam("userPswd") String userPswd,
                           Model model, HttpSession session){
        Admin admin=adminService.login(loginAcct,userPswd);
        if (admin==null){
            model.addAttribute(CrowdFundingConstant.ATTR_NAME_ERROR_MESSAGE,CrowdFundingConstant.MESSAGE_LOGIN_FAILED);
            return "admin-login";
        }else{
            session.setAttribute(CrowdFundingConstant.ATTR_NAME_LOGIN_ADMIN, admin);
            return "redirect:/admin/to/main/page";
        }
    }
    @ExceptionHandler(value=Exception.class)
    public String catchException(Exception exception,Model model) {
        model.addAttribute(CrowdFundingConstant.ATTR_NAME_ERROR_MESSAGE, exception);
        return "system-error";
    }
    @RequestMapping("/admin/query/for/search")
    public String queryForSearch(@RequestParam(value="pageNum", defaultValue="1") Integer pageNum,
                                 @RequestParam(value="pageSize", defaultValue="5",required = false) Integer pageSize,
                                 @RequestParam(value="keyword", defaultValue="",required = false) String keyword, Model model){
        PageInfo<Admin> pageInfo = adminService.queryForKeywordSearch(pageNum, pageSize, keyword);
        model.addAttribute(CrowdFundingConstant.ATTR_NAME_PAGE_INFO, pageInfo);
        System.out.println(pageInfo.toString());
        return "admin-page";
    }
    @RequestMapping("/admin/logout")
    public String logout(HttpSession session) {
        session.invalidate();
        return "redirect:/index";
    }
    @RequestMapping("/admin/batch/remove")
    @ResponseBody
    public ResultEntity<String> batchRemove(@RequestBody List<Integer> adminIdList){
        adminService.remove(adminIdList);
        return ResultEntity.successWithoutData();
    }
    @RequestMapping("/admin/save")
    public String saveAdmin(Admin admin) {
        adminService.saveAdmin(admin);
        return "redirect:/admin/query/for/search";
    }
    @RequestMapping("/admin/to/edit/page")
    public String toEditPage(@RequestParam("adminId") Integer adminId, Model model) {
    Admin admin=adminService.getAdminById(adminId);
    model.addAttribute(CrowdFundingConstant.ATTR_ADMIN,admin);
        return "admin-edit";
    }
    @RequestMapping("/admin/update")
    public String updateAdmin(Admin admin,Integer pageNum) {
    adminService.updateAdmin(admin);
    return "redirect:/admin/query/for/search?pageNum="+pageNum;
    }

}
